home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / cstream.exe / CSTREAM.HPP < prev   
C/C++ Source or Header  |  1991-06-07  |  2KB  |  111 lines

  1. /*
  2.  
  3.     cstream.hpp
  4.     6-7-91
  5.     class stream
  6.  
  7.     Copyright 1991
  8.     John W. Small
  9.     All rights reserved
  10.     Use freely but acknowledge authorship and copyright.
  11.     CIS: 73757,2233
  12.  
  13.  
  14.     PSW / Power SoftWare
  15.     P.O. Box 10072
  16.     McLean, Virginia 22102 8072
  17.     USA (703) 759-3838
  18.  
  19. */
  20.  
  21. #ifndef CSTREAM_HPP
  22. #define CSTREAM_HPP
  23.  
  24.  
  25. #include <iostream.h>
  26. #include <iomanip.h>
  27. #include <stdlib.h>
  28.  
  29. class StreamableClass;
  30. typedef StreamableClass *StreamC;
  31. #define StreamC0 ((StreamC)0)
  32.  
  33.  
  34. class StreamableClass  {
  35.     unsigned id;
  36. protected:
  37.     void setID(unsigned id)  { this->id = id; }
  38. public:
  39. static  char FieldTermChar;
  40.     unsigned ID() { return id; }
  41.     operator StreamC() { return (StreamC)this; }
  42. #pragma argsused
  43.     virtual void store(ostream& os) {return;}
  44.     StreamableClass(unsigned id)
  45.         { this->id = id; }
  46.     virtual ~StreamableClass() {return;}
  47. };
  48.  
  49. extern ostream& endf(ostream& os);
  50.  
  51.  
  52. #define StreamableClassID(class, base, id) \
  53. public:  \
  54.     enum { CLASS_ID = id };  \
  55. protected:  \
  56.     base :: setID;  \
  57.     class (unsigned cid = CLASS_ID) : base (cid) \
  58.         { return; }  \
  59. public: \
  60.     base :: ID;  \
  61.     operator StreamC() { return (StreamC)this; }  \
  62.     virtual void store(ostream& os);  \
  63.     static StreamC load(istream& is, StreamC C)
  64.  
  65.  
  66.  
  67. typedef struct {
  68.     unsigned id;
  69.     StreamC (*load)(istream& is, StreamC C);
  70. } StreamRecord;
  71.  
  72.  
  73. class StreamRegistry  {
  74. static StreamRecord recs[];
  75. static unsigned count, limit;
  76. public:
  77.     StreamRegistry()  { count = 0; }
  78.     void reg(unsigned id, StreamC (*loader)
  79.         (istream& is, StreamC C));
  80.     istream& get(istream& is, StreamC& C);
  81.     ostream& put(ostream& os, StreamC C);
  82.     virtual void error(char *msg, unsigned id)
  83.         { cerr << msg << id << endl; }
  84. };
  85.  
  86. extern StreamRegistry SR;
  87.  
  88. #define StreamableClasses(ClassCount)  \
  89.     StreamRecord StreamRegistry::recs[ClassCount]; \
  90.     unsigned StreamRegistry::count = 0; \
  91.     unsigned StreamRegistry::limit = ClassCount; \
  92.     StreamRegistry SR;
  93.  
  94. #define RegisterClass(ClassName)  \
  95.     SR.reg(ClassName::CLASS_ID, \
  96.     ClassName::load)
  97.  
  98.  
  99.  
  100. inline istream& operator>>(istream& is, StreamC& C)
  101. {
  102.     return SR.get(is,C);
  103. }
  104.  
  105. inline ostream& operator<<(ostream& os, StreamC C)
  106. {
  107.     return SR.put(os, C);
  108. }
  109.  
  110. #endif
  111.